﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NewsMine.DomainObjects;
using NewsMine.Storage;

namespace NewsDiscoveryEngine
{
    [Serializable]
    public class ScheduleHtml2FeedJob : BaseJob
    {
        public override bool ExecuteJob()
        {
            var menuItemsForCrawling = GetCrawlingMenuItemsFromWebStore();

            ProcessOutput(menuItemsForCrawling);

            return true;
        }

        public static List<WebSiteMenu> GetCrawlingMenuItemsFromWebStore()
        {
            var webSiteInfoStore = Defaults.WebSiteInfoMasterStore;

            webSiteInfoStore.Open();

            List<object> objs = webSiteInfoStore.Get(0, webSiteInfoStore.GetCount());

            if (objs == null || objs.Count <= 0)
                return null;

            List<WebSiteMenu> menuItemsForCrawling = new List<WebSiteMenu>();
            Dictionary<string,object> sitesForUpdatingBackToDatabase = new Dictionary<string,object>();

            foreach (var obj in objs)
            {
                WebSiteInfo wbSiteInfo = (WebSiteInfo)obj;

                if (wbSiteInfo.MenuItemconfigs == null || wbSiteInfo.MenuItemconfigs.Count <= 0)
                    continue;

                bool isAtleastOneMenuItemIsScheduledForCrawling = false;
                foreach (var menuItem in wbSiteInfo.MenuItemconfigs)
                {
                    int refreshFrequencyInSeconds = NewsMine.Configuration.CommonConstants.DefaultNewsRefreshFrequencyInSeconds;

                    if (menuItem.NewsRefreshFrequencyInSeconds > 0)
                        refreshFrequencyInSeconds = menuItem.NewsRefreshFrequencyInSeconds;

                    if (menuItem.LastCrawledTimeGMT.AddSeconds(refreshFrequencyInSeconds) < DateTime.UtcNow)
                    {
                        menuItemsForCrawling.Add(menuItem);
                        isAtleastOneMenuItemIsScheduledForCrawling = true;
                    }
                }

                if (isAtleastOneMenuItemIsScheduledForCrawling == true)
                {
                    if (sitesForUpdatingBackToDatabase.Keys.Contains<string>(wbSiteInfo.Domain) == false)
                        sitesForUpdatingBackToDatabase.Add(wbSiteInfo.Domain, wbSiteInfo);
                }
            }

            webSiteInfoStore.ReplaceMultiple(sitesForUpdatingBackToDatabase);
            webSiteInfoStore.Close();

            return menuItemsForCrawling;
        }
    }
}
